<script type="text/javascript">
	var $usersadd_content = $("#usersadd_content");

	$(document).ready(function () {
		var theme = 'fresh';
		$('.field_input').addClass('jqx-input');
		$('.field_input').addClass('field');
		$('.field_input').addClass('jqx-rc-all');

		var usersadd_save = $("#usersadd_save");
		var usersadd_cancel = $("#usersadd_cancel");
		var userspost_id = $("#userspost_id");
		var userspost_task = $("#userspost_task");
		var userspost_username = $("#userspost_username");
		var userspost_password = $("#userspost_password");
		var userspost_group = $("#userspost_group");
		var userspost_fullname = $("#userspost_fullname");
		var userspost_email = $("#userspost_email");
		var userspost_hp = $("#userspost_hp");

		var group_source = {
			id : 'id',
			datatype : "json",
			url : "index.php/c_group/group_list",
			root : "results",
			datafields : [
				{ name : 'id' },
				{ name : 'group_name' }
			],
			beforeprocessing : function(data){
				group_source.totalrecords = data[0].TotalRows;
			}
		}
		var group_dataAdapter = new $.jqx.dataAdapter(group_source,{
			loadComplete: function (){
				<?php
				if(isset($dataDetail)){
				?>
					userspost_group.find('input').val('<?php echo $dataDetail->group_name; ?>');
				<?php } ?>
			}
		});

		userspost_group.jqxComboBox({
			source : group_dataAdapter,
			theme : theme,
			width : 200,
			height : 25,
			autoDropDownHeight: true,
			displayMember : "group_name",
			valueMember : "id"
		});

		usersadd_save.jqxButton({ theme : theme, width : '70px', height : '18px' });
		usersadd_cancel.jqxButton({ width : '70px' , height : '18px' ,theme : theme });

		/* function */
		usersadd_cancel.click(function(){
			$('#mainpanelcontent').empty();
			$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#mainpanelcontent').load('index.php/c_users/users_load');
		});
		usersadd_save.click(function(){
			var id = "";
			var group_id = "";
			var username = "";
			var password = "";
			var email = "";
			var fullname = "";
			var handphone = "";

			id = userspost_id.val();
			if(userspost_group.jqxComboBox('getSelectedItem') !== null){
				group_id = userspost_group.jqxComboBox('getSelectedItem').value;
			}
			username = userspost_username.val();
			password = userspost_password.val();
			fullname = userspost_fullname.val();
			email = userspost_email.val();
			handphone = userspost_hp.val();
			task = userspost_task.val();

			if(task == ""){
				task = "create";
			}
			$.ajax({
				type : 'POST',
				url : 'index.php/c_users/users_save',
				data : {
					id : id,
					group_id : group_id,
					username : username,
					password : password,
					email : email,
					fullname : fullname,
					handphone : handphone,
					task : task
				},success : function(msg){
					var result = eval(msg);
					switch(result){
						case 1:
							$('#mainpanelcontent').empty();
							$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
							$('#mainpanelcontent').load('index.php/c_users/users_load');
							alert("Data berhasil disimpan");
							break;
						default:
							alert("Data gagal disimpan");
							break;
					}
				}
			});
		});

		<?php
			if(isset($dataDetail)){
		?>
			userspost_id.val('<?php echo $dataDetail->id; ?>');
			userspost_group.val('<?php echo $dataDetail->group_id; ?>');
			userspost_username.val('<?php echo $dataDetail->username; ?>');
			userspost_password.val('');
			userspost_fullname.val('<?php echo $dataDetail->fullname; ?>');
			userspost_email.val('<?php echo $dataDetail->email; ?>');
			userspost_hp.val('<?php echo $dataDetail->handphone; ?>');
			userspost_task.val('update');
		<?php
			}
		?>

	});

</script>
<!-- Begin content -->
<div class="modal_header">
		<span>Users Form</span>
	</div>
<div class="modal_content">
<div id="usersadd_content">
	<form action="" method="post" >
		<input type="hidden" id="userspost_id">
		<input type="hidden" id="userspost_task">
		<p>
			<label>Username:</label><br/>
			<input type="text" id="userspost_username" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Password:</label><br/>
			<input type="password" class="field_input" id="userspost_password" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Group:</label><br/>
			<div id="userspost_group"></div>
		</p>
		<br/>
		<p>
			<label>Fullname:</label><br/>
			<input type="text" id="userspost_fullname" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Email:</label><br/>
			<input type="text" id="userspost_email" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Handphone:</label><br/>
			<input type="text" id="userspost_hp" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<table align="left">
				<tr align="left">
					<td>
						<div id='usersadd_save'>
						<img style='float: left; margin: 0px;' src='./images/icons/save.gif' />
						<div style='float: left; margin-left: 4px;'>Simpan</div>
					</div>
					</td>
					<td>
						<div id='usersadd_cancel'>
						<img style='float: left; margin: 0px;' src='./images/icons/cross.gif' />
						<div style='float: left; margin-left: 4px;'>Cancel</div>
					</div>
					</td>
				</tr>
			</table>
		</p>
		<p>&nbsp;</p>
	</form>
</div>
</div>